<template>
    <div class="ele-body">
        <el-card shadow="never">
            <!-- 表格工具栏 -->
            <div class="ele-table-tool">
                <div class="ele-table-tool-left">
                    <el-button @click="showEdit=true" type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small">添加</el-button>
                    <el-button @click="mulRemove" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small">删除</el-button>
                </div>
            </div>
            <!-- 数据表格 -->
            <ele-data-table ref="table" :config="table" :choose.sync="choose" highlight-current-row>
                <template slot-scope="{index}">
                    <el-table-column type="selection" width="45" align="center" fixed="left"/>
                    <el-table-column type="index" label="序号" :index="index" width="60" align="center" fixed="left" show-overflow-tooltip/>
                    <el-table-column prop="name" label="姓名" show-overflow-tooltip width="300"/>
                    <el-table-column prop="create_time" label="添加时间" show-overflow-tooltip width="200"/>
                    <el-table-column prop="remark" label="备注" show-overflow-tooltip/>
                    <el-table-column label="操作" align="center" width="200px" :resizable="false" fixed="right">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row)" type="warning" size="mini" icon="el-icon-edit">修改</el-button>
                            <el-button @click="oneRemove(scope.row)" type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                        </template>
                    </el-table-column>
                </template>
            </ele-data-table>
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="editForm.id ? '修改类型' : '添加类型'" :visible.sync="showEdit" width="600px"
                :destroy-on-close="true" :lock-scroll="false" :close-on-click-modal="false" @close="editForm={}">
           <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="96px">
                <el-form-item label="名称:" prop="name">
                    <el-input v-model="editForm.name" placeholder="请输入假期名称" clearable/>
                </el-form-item>
                <el-form-item label="备注:">
                    <el-input type="textarea" v-model="editForm.remark" placeholder="请输入备注" rows="4" clearable/>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="showEdit=false">取消</el-button>
                <el-button type="primary" @click="save">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "Leave",
    data() {
        return {
            table: { // 表格配置
                url: '/api/attend/student/leave',
                where: {},
                page: true
            },
            choose: [],  // 表格选中数据
            editForm: {},
            showEdit: false,
            editRules: {
                name: [
                    {required: true, message: '请输入名称', trigger: 'blur'}
                ]
            }
        }
    },

    methods: {
        save() {
            this.$refs['editForm'].validate((valid) => {
                if (valid) {
                    const loading = this.$loading({lock: true});
                    const url = this.editForm.id > 0 ? '/api/attend/student/leave/edit' : '/api/attend/student/leave/add';
                    this.$http.post(url, this.editForm).then(res => {
                        loading.close();
                        if (res.data.code === 200) {
                            this.showEdit = false;
                            this.$message({type: 'success', message: res.data.msg});
                            this.$refs.table.reload();
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    }).catch(e => {
                        loading.close();
                        this.$message.error(e.message);
                    });
                } else {
                    return false;
                }
            });
        },

        edit(row) {
            this.editForm = Object.assign({}, row);
            this.showEdit = true;
        },

        delete(ids) {
            this.$confirm('确定要删除吗?', '提示', {type: 'warning'}).then(() => {
                const loading = this.$loading({lock: true});
                this.$http.post('/api/attend/student/leave/delete', {ids: ids}).then(res => {
                    loading.close();
                    if (res.data.code === 200) {
                        this.$message({type: 'success', message: res.data.msg});
                        this.$refs.table.reload();
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).catch(e => {
                    loading.close();
                    this.$message.error(e.message);
                });
            }).catch(() => 0);
        },

        mulRemove() {
            if (this.choose.length == 0) {
                this.$message.warning('请至少选择一条规则');
                return false;
            } 
            let ids = [];
            for(var i = 0; i< this.choose.length; i++) {
                console.log(this.choose[i]);
                ids.push(this.choose[i].id);
            }
            this.delete(ids);
        },

        oneRemove(row) {
            let ids = [];
            ids.push(row.id);
            this.delete(ids);
        }
    }
}
</script>